package com.yixin.my.shop.web.be.web.controller;

import com.yixin.my.shop.commons.dto.BaseResult;
import com.yixin.my.shop.commons.utils.RegexpUtils;
import com.yixin.my.shop.domain.TbUser;
import com.yixin.my.shop.web.be.service.TbUserService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.List;


@Controller
@RequestMapping(value = "user")
public class UserController {
    @Autowired
    private TbUserService tbUserService;

    @RequestMapping(value = "list",method = RequestMethod.GET)
    public String userList(Model model,TbUser tbUser){
        List<TbUser> tbUsers = tbUserService.search(tbUser);
        model.addAttribute("userlist",tbUsers);
        return "user/userlist";
    }

    @RequestMapping(value = "search",method = RequestMethod.POST)
    public String search(@RequestParam String username, @RequestParam String phone,@RequestParam String email, Model model){
        TbUser tbUser = new TbUser();
        tbUser.setUsername(username);
        tbUser.setPhone(phone);
        tbUser.setEmail(email);
        model.addAttribute("usersearchparam",tbUser);
        return userList(model,tbUser);
    }

    @RequestMapping(value = "form",method = RequestMethod.GET)
    public String userForm(Long id ,Model model){
        TbUser tbUser = tbUserService.getById(id);;
        if (tbUser == null){
            tbUser = new TbUser();
        }
        model.addAttribute("tbUser",tbUser);
        return "user/userform";
    }

    @RequestMapping(value = "save",method = RequestMethod.POST)
    public String save(TbUser tbUser,Model model){
        BaseResult baseResult = valide(tbUser);
        if (baseResult.isSuccess()){
            tbUserService.save(tbUser);
        }
        model.addAttribute(baseResult);
        model.addAttribute("tbUser",tbUser);
        return "user/userform";
    }

    private BaseResult valide(TbUser tbUser) {
        BaseResult baseResult = null;
        StringBuffer messages = new StringBuffer();
        if (tbUser == null){
            messages.append("请填写相关信息！<br/>");
        }
        if (StringUtils.isBlank(tbUser.getEmail())){
            messages.append("请输入邮箱！<br/>");
        } else if(!RegexpUtils.checkEmail(tbUser.getEmail())){
            messages.append("邮箱格式不正确！<br/>");
        }
        if (StringUtils.isBlank(tbUser.getPassword())){
            messages.append("请输入密码！<br/>");
        }
        if (StringUtils.isBlank(tbUser.getUsername())){
            messages.append("请输入用户名！<br/>");
        }
        if (StringUtils.isBlank(tbUser.getPhone())){
            messages.append("请输入手机号！<br/>");
        } else if(!RegexpUtils.checkPhone(tbUser.getPhone())){
            messages.append("手机号格式不正确！<br/>");
        }
        if (StringUtils.isBlank(messages.toString())){
            baseResult = BaseResult.success("保存成功！");
        } else{
            baseResult = BaseResult.fail(messages.toString());
        }
        return baseResult;
    }
}
